//
//  AppDelegate.swift
//  JLWeibo
//
//  Created by sunjinliang on 15/12/5.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

import UIKit
import AFNetworking
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
   
    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //添加活动指示器
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        setupAppearance()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultRootViewController
        window?.makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification, object: nil, queue: NSOperationQueue.mainQueue()) { (Notification) -> Void in
            let vc =  Notification.object == nil ? MainViewController() : WelcomeViewController()
            self.window?.rootViewController = vc
        }
        return true
    }
/// 设置文字渲染颜色r
    func setupAppearance(){
         UITabBar.appearance().tintColor = WBAppearanceTintColor
         UINavigationBar.appearance().tintColor = WBAppearanceTintColor
    }
}
extension AppDelegate{
    private var defaultRootViewController: UIViewController{
        if UserAccountVM.shareUserAccount.userLogon{
           return isNewVersion ? NewFeatureCollectionViewController() : WelcomeViewController()
        }
        return MainViewController()
        
    }
    
    private var isNewVersion: Bool {
        //当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currentVersion)
    //   print("当前版本号\(version)")
        //上次版本号
        let lastVersionKey = "WBVersion"
        let lastVersion = NSUserDefaults.standardUserDefaults().doubleForKey(lastVersionKey)
        NSUserDefaults.standardUserDefaults().setDouble(version!, forKey:lastVersionKey)
      //  print("上次版本号\(lastVersion)")
        return version > lastVersion
    }
func applicationDidEnterBackground(application: UIApplication) {
    
    StatusDAL.clearDateCache()
    }
    
    
}